#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 5e4 + 10;
int f[N * 3];
int n, k;
int ret;
int find(int x)
{
    return f[x] == x ? x : f[x] = find(f[x]);
}
void un(int x, int y)
{
    f[find(y)] = find(x);
}
int main()
{
    cin >> n >> k;
    for (int i = 1; i <= 3 * n; i++)
    {
        f[i] = i;
    }
    while (k--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (b > n || c > n)
        {
            ret++;
        }
        else if (a == 1)
        {
            if (find(b) == find(c + n) || find(b) == find(c + 2 * n))
            {
                ret++;
            }
            else
            {
                un(b, c);
                un(b + n, c + n);
                un(b + 2 * n, c + 2 * n);
            }
        }
        else
        {
            if (find(b) == find(c) || find(b) == find(c + n))
            {
                ret++;
            }
            else
            {
                un(b, c + 2 * n);
                un(b + n, c);
                un(b + 2 * n, c + n);
            }
        }
    }
    cout << ret << endl;
    return 0;
}